package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.otaliastudios.cameraview.BitmapCallback;
import com.otaliastudios.cameraview.CameraException;
import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraOptions;
import com.otaliastudios.cameraview.PictureResult;
import com.otaliastudios.cameraview.VideoResult;
import com.otaliastudios.cameraview.controls.Audio;
import com.otaliastudios.cameraview.controls.Facing;
import com.otaliastudios.cameraview.controls.Flash;
import com.otaliastudios.cameraview.controls.Mode;
import com.otaliastudios.cameraview.filter.Filters;
import com.otaliastudios.cameraview.size.SizeSelector;
import com.otaliastudios.cameraview.size.SizeSelectors;
import com.stark.weather.lib.WeatherManager;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CameraFilterAdapter;
import flc.ast.adapter.MarkAdapter;
import flc.ast.bean.MyCameraFilterBean;
import flc.ast.bean.MyMarkBean;
import flc.ast.databinding.ActivityCameraBinding;
import flc.ast.utils.Content;
import stark.common.apis.base.Weather;
import stark.common.base.IReqRetCallback;
import stark.common.basic.utils.DensityUtil;
import stark.common.basic.utils.LocationUtil;
import stark.common.basic.utils.SPUtil;
import stark.common.basic.utils.StkPermissionHelper;
import stark.common.basic.utils.WorkPathUtil;

public class CameraActivity extends BaseAc<ActivityCameraBinding> {

    public static int selPos;
    private boolean isLocation = false;//是否获取定位
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1:
                    isLocation = false;
                    break;
                case 2:
                    isLocation = true;
                    dismissDialog();
                    ToastUtils.showShort(R.string.get_location_suc);
                    stopLoc();
                    break;
            }
            return false;
        }
    });
    //数据
    private String location = "";
    private String province = "";
    private String city = "";
    private String district = "";
    private String street = "";
    private String latitude = "";
    private String longitude = "";
    private String weather = "";
    private String temperature = "";
    private int latitudeNum, longitudeNum;

    //相机设置
    private boolean isFlash = false;//判断是否打开闪光灯
    //滤镜
    private CameraFilterAdapter filterAdapter;
    private int oldFilterPos = 0;
    //水印
    private MarkAdapter markAdapter;
    private int selMarkPos = 0;

    @Override
    protected int onCreate() {
        return R.layout.activity_camera;
    }

    @Override
    protected void initView() {
        mDataBinding.ivCameraBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.llCamera.setOnClickListener(this);
        mDataBinding.llMark1.setOnClickListener(this);
        mDataBinding.llMark2.setOnClickListener(this);
        mDataBinding.llMark3.setOnClickListener(this);
        mDataBinding.llMark4.setOnClickListener(this);
        mDataBinding.llMark5.setOnClickListener(this);
        mDataBinding.llMark6.setOnClickListener(this);
        mDataBinding.llMark7.setOnClickListener(this);
        mDataBinding.llMark8.setOnClickListener(this);
        mDataBinding.llMark9.setOnClickListener(this);
        mDataBinding.llMark10.setOnClickListener(this);
        mDataBinding.llMark11.setOnClickListener(this);
        mDataBinding.llMark12.setOnClickListener(this);
        mDataBinding.llMark13.setOnClickListener(this);
        mDataBinding.llMark14.setOnClickListener(this);
        mDataBinding.ivCameraFlash.setOnClickListener(this);
        mDataBinding.ivCameraReversal.setOnClickListener(this);
        mDataBinding.ivCameraStart.setOnClickListener(this);
        mDataBinding.tvCameraFilter.setOnClickListener(this);
        mDataBinding.tvCameraMark.setOnClickListener(this);

        mDataBinding.rvFilterList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        filterAdapter = new CameraFilterAdapter();
        mDataBinding.rvFilterList.setAdapter(filterAdapter);
        filterAdapter.setOnItemClickListener(this);

        mDataBinding.rvMarkList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        markAdapter = new MarkAdapter();
        mDataBinding.rvMarkList.setAdapter(markAdapter);
        markAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.llCamera:
            case R.id.llMark1:
            case R.id.llMark2:
            case R.id.llMark3:
            case R.id.llMark4:
            case R.id.llMark5:
            case R.id.llMark6:
            case R.id.llMark7:
            case R.id.llMark8:
            case R.id.llMark9:
            case R.id.llMark10:
            case R.id.llMark11:
            case R.id.llMark12:
            case R.id.llMark13:
            case R.id.llMark14:
                mDataBinding.llBottomMark.setVisibility(View.GONE);
                mDataBinding.llBottomFilter.setVisibility(View.GONE);
                break;
            case R.id.ivCameraFlash://闪光灯
                openFlash();
                break;
            case R.id.ivCameraReversal://镜头反转
                reversalLens();
                break;
            case R.id.tvCameraFilter://滤镜
                mDataBinding.llBottomMark.setVisibility(View.GONE);
                if (mDataBinding.llBottomFilter.getVisibility() == View.GONE) {
                    mDataBinding.llBottomFilter.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.llBottomFilter.setVisibility(View.GONE);
                }
                break;
            case R.id.tvCameraMark://水印
                //保存了位置信息后不重复获取
                location = SPUtil.getString(mContext, Content.Location, "");
                if (TextUtils.isEmpty(location)) {
                    isLocation = false;
                    getWeatherByMap();
                } else {
                    isLocation = true;
                    String[] split = location.split("/");
                    province = split[0];
                    city = split[1];
                    district = split[2];
                    street = split[3];
                    latitude = split[4];
                    longitude = split[5];
                    weather = split[6];
                    temperature = split[7];
                }
                if (!isLocation) {
                    return;
                }
                mDataBinding.llBottomFilter.setVisibility(View.GONE);
                if (mDataBinding.llBottomMark.getVisibility() == View.GONE) {
                    mDataBinding.llBottomMark.setVisibility(View.VISIBLE);
                } else {
                    mDataBinding.llBottomMark.setVisibility(View.GONE);
                }
                break;
            case R.id.ivCameraStart://开始拍摄
                startShot();
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        if (adapter == filterAdapter) {
            //设置滤镜
            filterAdapter.getItem(oldFilterPos).setSelect(false);
            oldFilterPos = position;
            filterAdapter.getItem(position).setSelect(true);
            filterAdapter.notifyDataSetChanged();
            mDataBinding.cameraView.setFilter(filterAdapter.getItem(position).getFilter());
        } else if (adapter == markAdapter) {
            markAdapter.getItem(selMarkPos).setSelect(false);
            markAdapter.getItem(position).setSelect(true);
            markAdapter.notifyDataSetChanged();
            selMarkPos = position;
            setMark(selMarkPos);
        }
    }

    @Override
    protected void initData() {
        getPermission();
        //滤镜数据
        List<MyCameraFilterBean> mFilterList = new ArrayList<>();//滤镜集合
        mFilterList.add(new MyCameraFilterBean(getString(R.string.camera_filter_text1), "#00000000", Filters.values()[0].newInstance(), true));
        mFilterList.add(new MyCameraFilterBean(getString(R.string.camera_filter_text2), "#323333", Filters.values()[1].newInstance(), false));
        mFilterList.add(new MyCameraFilterBean(getString(R.string.camera_filter_text3), "#C376F3", Filters.values()[2].newInstance(), false));
        mFilterList.add(new MyCameraFilterBean(getString(R.string.camera_filter_text4), "#6F7171", Filters.values()[3].newInstance(), false));
        mFilterList.add(new MyCameraFilterBean(getString(R.string.camera_filter_text5), "#505151", Filters.values()[4].newInstance(), false));
        mFilterList.add(new MyCameraFilterBean(getString(R.string.camera_filter_text6), "#999999", Filters.values()[5].newInstance(), false));
        mFilterList.add(new MyCameraFilterBean(getString(R.string.camera_filter_text7), "#513232", Filters.values()[6].newInstance(), false));
        mFilterList.add(new MyCameraFilterBean(getString(R.string.camera_filter_text8), "#787A7A", Filters.values()[7].newInstance(), false));
        filterAdapter.setList(mFilterList);
        //水印数据
        List<MyMarkBean> mMarkList = new ArrayList<>();
        mMarkList.add(new MyMarkBean(R.drawable.wsy1, false));
        mMarkList.add(new MyMarkBean(R.drawable.y1, false));
        mMarkList.add(new MyMarkBean(R.drawable.y2, false));
        mMarkList.add(new MyMarkBean(R.drawable.y3, false));
        mMarkList.add(new MyMarkBean(R.drawable.y4, false));
        mMarkList.add(new MyMarkBean(R.drawable.y5, false));
        mMarkList.add(new MyMarkBean(R.drawable.y6, false));
        mMarkList.add(new MyMarkBean(R.drawable.y7, false));
        mMarkList.add(new MyMarkBean(R.drawable.y8, false));
        mMarkList.add(new MyMarkBean(R.drawable.y9, false));
        mMarkList.add(new MyMarkBean(R.drawable.y10, false));
        mMarkList.add(new MyMarkBean(R.drawable.y11, false));
        mMarkList.add(new MyMarkBean(R.drawable.y12, false));
        mMarkList.add(new MyMarkBean(R.drawable.y13, false));
        mMarkList.add(new MyMarkBean(R.drawable.y14, false));
        markAdapter.setList(mMarkList);
        //设置默认选中水印布局
//        if (isLocation) {
//            mMarkList.get(0).setSelect(true);//默认选中水印
//            selMarkPos = 0;
//            setMark(selMarkPos);
//        }
        if (selPos != -1) {
            //保存了位置信息后不重复获取
            location = SPUtil.getString(mContext, Content.Location, "");
            if (TextUtils.isEmpty(location)) {
                isLocation = false;
                getWeatherByMap();
            } else {
                isLocation = true;
                String[] split = location.split("/");
                province = split[0];
                city = split[1];
                district = split[2];
                street = split[3];
                latitude = split[4];
                longitude = split[5];
                weather = split[6];
                temperature = split[7];
            }
            if (!isLocation) {
                return;
            }
            selMarkPos = selPos;
            mMarkList.get(selMarkPos).setSelect(true);//默认选中水印
            setMark(selMarkPos);
        } else {
            mMarkList.get(0).setSelect(true);//默认选中水印
            selMarkPos = 0;
            setMark(selMarkPos);
        }
    }

    private void getPermission() {
        StkPermissionHelper.permission(Manifest.permission.CAMERA).reqPermissionDesc(getString(R.string.get_camera_permission)).callback(new StkPermissionHelper.ACallback() {
            @Override
            public void onGranted() {
                //初始化定位天气
                WeatherManager.initCityData(new WeatherManager.IInitCallback() {
                    @Override
                    public void onRet(boolean success) {
                        if (!success) return;
                        //初始化相机
                        initCameraView();
                    }
                });
            }
        }).request();
    }

    //初始化相机
    private void initCameraView() {
        mDataBinding.cameraView.setMode(Mode.PICTURE);
        mDataBinding.cameraView.setAudio(Audio.OFF);
        mDataBinding.cameraView.setLifecycleOwner(this);
        int width = DensityUtil.getWith(mContext);
        int height = DensityUtil.getHeight(mContext);
        SizeSelector maxAreaSelector = SizeSelectors.maxArea(width * height);
        SizeSelector equalWidthSelector = SizeSelectors.withFilter(size -> size.getWidth() == width);
        mDataBinding.cameraView.setPictureSize(SizeSelectors.and(maxAreaSelector, equalWidthSelector));
        mDataBinding.cameraView.addCameraListener(new CameraListener() {
            @Override
            public void onCameraOpened(@NonNull CameraOptions options) {

            }

            @Override
            public void onCameraClosed() {
            }

            @Override
            public void onCameraError(@NonNull CameraException exception) {
            }

            @Override
            public void onPictureTaken(@NonNull PictureResult result) {
                if (selMarkPos != 0) {
                    //水印处理 核心点1：计算水印控件在与CameraView所属同一级容器内的位置的比例
                    PreviewActivity.watermarkLeftRate = getLeft();
                    PreviewActivity.watermarkTopRate = getTop();
                    PreviewActivity.watermarkBitmap = getWatermarkBitmap();
                }

                //去到预览界面
                PreviewActivity.sPicResult = result;
                startActivity(PreviewActivity.class);
            }

            @Override
            public void onVideoRecordingStart() {

            }

            @Override
            public void onVideoRecordingEnd() {

            }

            @Override
            public void onVideoTaken(@NonNull VideoResult result) {

            }
        });
    }

    //开始拍摄
    private void startShot() {
        if (mDataBinding.cameraView.isTakingPicture()) return;
        mDataBinding.cameraView.takePictureSnapshot();
    }

    //反转镜头
    private void reversalLens() {
        Facing curFacing = mDataBinding.cameraView.getFacing();
        if (curFacing == Facing.BACK) {
            mDataBinding.cameraView.setFacing(Facing.FRONT);
        } else {
            mDataBinding.cameraView.setFacing(Facing.BACK);
        }
    }

    //打开闪光灯
    private void openFlash() {
        if (isFlash) {
            isFlash = false;
            mDataBinding.ivCameraFlash.setImageResource(R.drawable.sgd2);
            shotTipText(getString(R.string.flash_close));
            mDataBinding.cameraView.setFlash(Flash.OFF);
        } else {
            isFlash = true;
            mDataBinding.ivCameraFlash.setImageResource(R.drawable.sgd1);
            shotTipText(getString(R.string.flash_open));
            mDataBinding.cameraView.setFlash(Flash.TORCH);
        }
    }

    //显示提示文字
    private void shotTipText(String text) {
        mDataBinding.tvCameraTipText.setText(text);
        mDataBinding.tvCameraTipText.setVisibility(View.VISIBLE);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                mDataBinding.tvCameraTipText.setVisibility(View.GONE);
            }
        }, 500);
    }

    //获取权限
    private void getWeatherByMap() {
        boolean isEnabled = LocationUtil.isLocationEnabled();
        if (!isEnabled) {
            ToastUtils.showShort(R.string.please_open_location);
            return;
        }
        StkPermissionHelper.permission(Manifest.permission.ACCESS_FINE_LOCATION)
                .reqPermissionDesc(getString(R.string.get_location_permission))
                .callback(new StkPermissionHelper.ACallback() {
                    @Override
                    public void onGranted() {
                        startLoc();
                    }
                })
                .request();
    }

    //开始定位
    private AMapLocationClient client;

    private void startLoc() {
        if (client != null) {
            return;
        }
        showDialog(getString(R.string.get_location_ing));
        client = new AMapLocationClient(mContext);
        AMapLocationClientOption clientOption = new AMapLocationClientOption();
        clientOption.setInterval(3000);
        clientOption.setNeedAddress(true);
        client.setLocationOption(clientOption);
        client.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation == null) return;
                int errCode = aMapLocation.getErrorCode();
                if (errCode != 0) return;
                //获取位置信息
                province = aMapLocation.getProvince();
                city = aMapLocation.getCity();
                district = aMapLocation.getDistrict();
                street = aMapLocation.getStreet();
                latitude = (int) aMapLocation.getLatitude() + "°";
                longitude = (int) aMapLocation.getLongitude() + "°";
                latitudeNum = (int) aMapLocation.getLatitude();
                longitudeNum = (int) aMapLocation.getLongitude();
                //根据城市名称来获取天气信息
                getWeather(aMapLocation.getCity());
                if (TextUtils.isEmpty(city) || TextUtils.isEmpty(weather)) {
                    handler.sendEmptyMessage(1);//还未获取到定位信息
                } else {
                    location = province + "/" + city + "/" + district + "/" + street + "/" + latitude +
                            "/" + longitude + "/" + weather + "/" + temperature;
                    SPUtil.putString(mContext, Content.Location, location);
                    handler.sendEmptyMessage(2);//已获取定位信息
                }
            }
        });
        client.startLocation();
    }

    //停止获取定位
    private void stopLoc() {
        if (client != null) {
            client.stopLocation();
            client.onDestroy();
            client = null;
        }
    }

    //获取天气
    private void getWeather(String city) {
        WeatherManager.getWeather(this, city, new IReqRetCallback<Weather>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable Weather result) {
                if (success && result != null) {
                    weather = result.future.get(0).weather;//获取当天天气
                    int index = result.realtime.temperature.lastIndexOf('/');
                    if (index != -1) {
                        temperature = result.realtime.temperature.substring(0, index) + getString(R.string.unit_temperature);//获取当天温度
                    } else {
                        temperature = result.realtime.temperature + getString(R.string.unit_temperature);//获取当天温度
                    }

                }
            }
        });
    }

    //设置水印
    private void setMark(int pos) {
        switch (pos) {
            case 0:
                clearView();
                break;
            case 1:
                clearView();
                mDataBinding.llMark1.setVisibility(View.VISIBLE);
                mDataBinding.tvMark1Time.setText(TimeUtils.getNowString(new SimpleDateFormat("HH:mm")));
                mDataBinding.tvMark1Location.setText(city + "·" + district);
                mDataBinding.tvMark1Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd")));
                mDataBinding.tvMark1Weather.setText(TimeUtils.getNowString(new SimpleDateFormat("EEE")) + " " + weather + " " + temperature);
                break;
            case 2:
                clearView();
                mDataBinding.llMark2.setVisibility(View.VISIBLE);
                mDataBinding.tvMark2Time.setText(TimeUtils.getNowString(new SimpleDateFormat("HH:mm")));
                mDataBinding.tvMark2Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd EEE")));
                mDataBinding.tvMark2Location.setText(city + "·" + district);
                mDataBinding.tvMark2Weather.setText(weather + temperature);
                break;
            case 3:
                clearView();
                mDataBinding.llMark3.setVisibility(View.VISIBLE);
                mDataBinding.tvMark3Weather.setText(weather);
                mDataBinding.tvMark3Temperature.setText(temperature);
                mDataBinding.tvMark3Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd EEE")));
                mDataBinding.tvMark3Location.setText(city + "·" + district);
                break;
            case 4:
                clearView();
                mDataBinding.llMark4.setVisibility(View.VISIBLE);
                mDataBinding.tvMark4Time.setText(TimeUtils.getNowString(new SimpleDateFormat("HH:mm")));
                mDataBinding.tvMark4Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy月MM日dd EEE")));
                mDataBinding.tvMark4Location.setText(city + "·" + district);
                break;
            case 5:
                clearView();
                mDataBinding.llMark5.setVisibility(View.VISIBLE);
                mDataBinding.tvMark5Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy.MM.dd")));
                mDataBinding.tvMark5Time.setText(TimeUtils.getNowString(new SimpleDateFormat("HH:mm:ss")));
                mDataBinding.tvMark5Location.setText(city + "·" + district);
                mDataBinding.tvMark5Weather.setText(weather);
                mDataBinding.tvMark5Temperature.setText(temperature);
                break;
            case 6:
                clearView();
                mDataBinding.llMark6.setVisibility(View.VISIBLE);
                mDataBinding.tvMark6Time.setText(TimeUtils.getNowString(new SimpleDateFormat("HH:mm")));
                mDataBinding.tvMark6Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy.MM.dd")));
                mDataBinding.tvMark6Week.setText(TimeUtils.getNowString(new SimpleDateFormat("EEE")));
                mDataBinding.tvMark6Location.setText(city + "·" + district + "·" + street);
                break;
            case 7:
                clearView();
                mDataBinding.llMark7.setVisibility(View.VISIBLE);
                mDataBinding.tvMark7Time.setText(TimeUtils.getNowString(new SimpleDateFormat("HH:mm")));
                mDataBinding.tvMark7Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy.MM.dd EEE ")));
                mDataBinding.tvMark7Location.setText("\uD83D\uDCCD" + city + "·" + district);
                break;
            case 8:
                clearView();
                mDataBinding.llMark8.setVisibility(View.VISIBLE);
                mDataBinding.tvMark8Location.setText(city + "·" + district);
                mDataBinding.tvMark8Time.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd HH:mm")));
                String latitudeStr = latitudeNum == 0 ? getString(R.string.northern_latitude) : getString(R.string.south_latitude);
                String longitudeStr = longitudeNum == 0 ? getString(R.string.east_longitude) : getString(R.string.west_longitude);
                mDataBinding.tvMark8Latitude.setText(longitudeStr + longitude + " " + latitudeStr + latitude);
                break;
            case 9:
                clearView();
                mDataBinding.llMark9.setVisibility(View.VISIBLE);
                mDataBinding.tvMark9Time.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy.MM.dd EEE HH:mm")));
                mDataBinding.tvMark9Weather.setText(weather + temperature);
                mDataBinding.tvMark9Location.setText(city + "·" + district + "·" + street);
                break;
            case 10:
                clearView();
                mDataBinding.llMark10.setVisibility(View.VISIBLE);
                mDataBinding.tvMark10Location.setText(city + "·" + district);
                String latitudeStr1 = latitudeNum == 0 ? "N" : "S";
                String longitudeStr1 = longitudeNum == 0 ? "E" : "W";
                mDataBinding.tvMark10Latitude.setText(getString(R.string.latitude_text) + latitude + latitudeStr1 + " " + longitude + longitudeStr1);
                mDataBinding.tvMark10Weather.setText(getString(R.string.weather_text) + weather + temperature);
                mDataBinding.tvMark10Time.setText(getString(R.string.time_text) + TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd EEE HH:mm")));
                break;
            case 11:
                clearView();
                mDataBinding.llMark11.setVisibility(View.VISIBLE);
                mDataBinding.tvMark11Time.setText(TimeUtils.getNowString(new SimpleDateFormat("HH:mm")));
                mDataBinding.tvMark11Location.setText(city + "·" + district);
                mDataBinding.tvMark11Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy.MM.dd EEE")));
                mDataBinding.tvMark11Weather.setText(weather + temperature);
                break;
            case 12:
                clearView();
                mDataBinding.llMark12.setVisibility(View.VISIBLE);
                mDataBinding.tvMark12Week.setText(TimeUtils.getNowString(new SimpleDateFormat("EEE")));
                mDataBinding.tvMark12Weather.setText(weather + temperature);
                mDataBinding.tvMark12Location.setText(city + "·" + district);
                mDataBinding.tvMark12Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy.MM.dd")));
                mDataBinding.tvMark12Time.setText(TimeUtils.getNowString(new SimpleDateFormat("HH:mm")));
                break;
            case 13:
                clearView();
                mDataBinding.llMark13.setVisibility(View.VISIBLE);
                mDataBinding.tvMark13Time.setText(TimeUtils.getNowString(new SimpleDateFormat("HH:mm")));
                mDataBinding.tvMark13Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy-MM-dd EEE")) + " " + weather + temperature);
                mDataBinding.tvMark13Location.setText(city + "·" + district);
                break;
            case 14:
                clearView();
                mDataBinding.llMark14.setVisibility(View.VISIBLE);
                mDataBinding.tvMark14Time.setText(TimeUtils.getNowString(new SimpleDateFormat("HH:mm")));
                mDataBinding.tvMark14Location.setText(city + "·" + district + "·" + street);
                mDataBinding.tvMark14Date.setText(TimeUtils.getNowString(new SimpleDateFormat("yyyy.MM.dd EEE")));
                break;
        }
    }

    //获取left
    private float getLeft() {
        float leftRate = 0;
        switch (selMarkPos) {
            case 1:
                leftRate = mDataBinding.llMark1.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 2:
                leftRate = mDataBinding.llMark2.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 3:
                leftRate = mDataBinding.llMark3.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 4:
                leftRate = mDataBinding.llMark4.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 5:
                leftRate = mDataBinding.llMark5.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 6:
                leftRate = mDataBinding.llMark6.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 7:
                leftRate = mDataBinding.llMark7.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 8:
                leftRate = mDataBinding.llMark8.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 9:
                leftRate = mDataBinding.llMark9.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 10:
                leftRate = mDataBinding.llMark10.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 11:
                leftRate = mDataBinding.llMark11.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 12:
                leftRate = mDataBinding.llMark12.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 13:
                leftRate = mDataBinding.llMark13.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
            case 14:
                leftRate = mDataBinding.llMark14.getLeft() * 1f / mDataBinding.rlCamera.getWidth();
                break;
        }
        return leftRate;
    }

    //获取top
    private float getTop() {
        float topRate = 0;
        switch (selMarkPos) {
            case 1:
                topRate = mDataBinding.llMark1.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 2:
                topRate = mDataBinding.llMark2.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 3:
                topRate = mDataBinding.llMark3.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 4:
                topRate = mDataBinding.llMark4.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 5:
                topRate = mDataBinding.llMark5.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 6:
                topRate = mDataBinding.llMark6.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 7:
                topRate = mDataBinding.llMark7.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 8:
                topRate = mDataBinding.llMark8.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 9:
                topRate = mDataBinding.llMark9.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 10:
                topRate = mDataBinding.llMark10.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 11:
                topRate = mDataBinding.llMark11.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 12:
                topRate = mDataBinding.llMark12.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 13:
                topRate = mDataBinding.llMark13.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
            case 14:
                topRate = mDataBinding.llMark14.getTop() * 1f / mDataBinding.rlCamera.getHeight();
                break;
        }
        return topRate;
    }

    //获取水印图片
    private Bitmap getWatermarkBitmap() {
        Bitmap bitmap = null;
        switch (selMarkPos) {
            case 1:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark1);
                break;
            case 2:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark2);
                break;
            case 3:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark3);
                break;
            case 4:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark4);
                break;
            case 5:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark5);
                break;
            case 6:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark6);
                break;
            case 7:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark7);
                break;
            case 8:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark8);
                break;
            case 9:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark9);
                break;
            case 10:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark10);
                break;
            case 11:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark11);
                break;
            case 12:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark12);
                break;
            case 13:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark13);
                break;
            case 14:
                bitmap = ImageUtils.view2Bitmap(mDataBinding.llMark14);
                break;
        }
        return bitmap;
    }

    //管理控件
    private void clearView() {
        mDataBinding.llMark1.setVisibility(View.GONE);
        mDataBinding.llMark2.setVisibility(View.GONE);
        mDataBinding.llMark3.setVisibility(View.GONE);
        mDataBinding.llMark4.setVisibility(View.GONE);
        mDataBinding.llMark5.setVisibility(View.GONE);
        mDataBinding.llMark6.setVisibility(View.GONE);
        mDataBinding.llMark7.setVisibility(View.GONE);
        mDataBinding.llMark8.setVisibility(View.GONE);
        mDataBinding.llMark9.setVisibility(View.GONE);
        mDataBinding.llMark10.setVisibility(View.GONE);
        mDataBinding.llMark11.setVisibility(View.GONE);
        mDataBinding.llMark12.setVisibility(View.GONE);
        mDataBinding.llMark13.setVisibility(View.GONE);
        mDataBinding.llMark14.setVisibility(View.GONE);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopLoc();
    }
}